function getCookie(name) {
    var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
    return r ? r[1] : undefined;
}


$(function () {


    // 发布电影评论
    $("#set_comment").click(function (e) {
        var comment_value = $("#comment_value").val();
        var film_id = $('.span').attr("id");
        var params = {
            "comment_value": comment_value,
            "film_id":film_id,
        };

        $.ajax({
                url: "/comment/",
                type: "post",
                data: JSON.stringify(params),
                contentType: "application/json",
                headers: {
                    "X-CSRFToken": getCookie("csrf_token")
                },
                success: function (resp) {
                    if (resp.errno == "0") {
                        alert(resp.errmsg);
                        window.location.reload()  //刷新页面
                    } else {
                        alert(resp.errmsg);
                    }
                }
            })


    });

    $(".like_user").click(function (e) {
        var active_like = 1;
        var song_id = $(this).attr("song_id");
        var singer_id = $(this).attr("singer_id");
        var singer_name = $(this).attr("singer_name");
        var album_name = $(this).attr("album_name");
        var album_id = $(this).attr("album_id");
        var song_name = $(this).attr("song_name");
        var user_id = $(this).attr("user_id");

        var params = {
            "active_like": active_like,
            'song_id': song_id,
            'singer_name': singer_name,
            'singer_id': singer_id,
            'album_name': album_name,
            'album_id': album_id,
            'song_name': song_name,
            'user_id': user_id,

        };


        $.ajax({
                url: "/song_details/",
                type: "post",
                data: JSON.stringify(params),
                contentType: "application/json",
                headers: {
                    "X-CSRFToken": getCookie("csrf_token")
                },
                success: function (resp) {
                    if (resp.errno == "0") {

                        alert(resp.errmsg);
                        window.location.reload()  //刷新页面
                    } else {
                        alert(resp.errmsg);
                    }
                }
            })


    });

    $(".del_like").click(function (e) {
        var del_like = 0;
        var song_id = $(this).attr("song_id");
        var params = {
            "del_like": del_like,
            'song_id': song_id,
        };


        $.ajax({
                url: "/song_details/",
                type: "delete",
                data: JSON.stringify(params),
                contentType: "application/json",
                headers: {
                    "X-CSRFToken": getCookie("csrf_token")
                },
                success: function (resp) {
                    if (resp.errno == "0") {
                        alert(resp.errmsg);
                        window.location.reload()  //刷新页面
                    } else {
                        alert(resp.errmsg);
                    }
                }
            })


    });

    $(".like_singer").click(function (e) {
        var active_like = 1;
        var singer_id = $(this).attr("singer_id");
        var singer_name = $(this).attr("singer_name");
        var singer_img = $(this).attr("singer_img");
        var user_id = $(this).attr("user_id");


        var params = {
            "singer_img": singer_img,
            'singer_name': singer_name,
            'singer_id': singer_id,
            'active_like': active_like,
            'user_id': user_id,

        };


        $.ajax({
                url: "/singers_detail/",
                type: "post",
                data: JSON.stringify(params),
                contentType: "application/json",
                headers: {
                    "X-CSRFToken": getCookie("csrf_token")
                },
                success: function (resp) {
                    if (resp.errno == "0") {

                        alert(resp.errmsg);
                        window.location.reload()  //刷新页面
                    } else {
                        alert(resp.errmsg);
                    }
                }
            })


    });

    $(".del_singer").click(function (e) {
        var del_like = 1;
        var song_id = $(this).attr("song_id");
        var user_id = $(this).attr("user_id");
        var params = {
            "del_like": del_like,
            'song_id': song_id,
            'user_id': user_id,
        };


        $.ajax({
                url: "/singers_detail/",
                type: "delete",
                data: JSON.stringify(params),
                contentType: "application/json",
                headers: {
                    "X-CSRFToken": getCookie("csrf_token")
                },
                success: function (resp) {
                    if (resp.errno == "0") {
                        alert(resp.errmsg);
                        window.location.reload()  //刷新页面
                    } else {
                        alert(resp.errmsg);
                    }
                }
            })


    });

    $(".like_song_list").click(function (e) {
        var active_like = 1;
        var playlist_id = $(this).attr("playlist_id");
        var playlist_name = $(this).attr("playlist_name");
        var playlist_img = $(this).attr("playlist_img");
        var playlist_song_count = $(this).attr("playlist_song_count");
        var playlist_play_count = $(this).attr("playlist_play_count");
        var user_id = $(this).attr("user_id");


        var params = {
            "playlist_id": playlist_id,
            'playlist_name': playlist_name,
            'playlist_img': playlist_img,
            'playlist_song_count': playlist_song_count,
            'playlist_play_count': playlist_play_count,
            'user_id': user_id,
            'active_like': active_like
        };


        $.ajax({
                url: "/song_list/",
                type: "post",
                data: JSON.stringify(params),
                contentType: "application/json",
                headers: {
                    "X-CSRFToken": getCookie("csrf_token")
                },
                success: function (resp) {
                    if (resp.errno == "0") {

                        alert(resp.errmsg);
                        window.location.reload()  //刷新页面
                    } else {
                        alert(resp.errmsg);
                    }
                }
            })


    });

    $(".del_song_like").click(function (e) {
        var del_like = 2;
        var playlist_id = $(this).attr("playlist_id");
        var user_id = $(this).attr("user_id");
        var params = {
            "del_like": del_like,
            'playlist_id': playlist_id,
            'user_id': user_id,
        };


        $.ajax({
                url: "/song_list/",
                type: "delete",
                data: JSON.stringify(params),
                contentType: "application/json",
                headers: {
                    "X-CSRFToken": getCookie("csrf_token")
                },
                success: function (resp) {
                    if (resp.errno == "0") {
                        alert(resp.errmsg);
                        window.location.reload()  //刷新页面
                    } else {
                        alert(resp.errmsg);
                    }
                }
            })


    });

    $(".like_album").click(function (e) {
        var active_like = 1;
        var album_id = $(this).attr("album_id");
        var album_name = $(this).attr("album_name");
        var album_pic = $(this).attr("album_pic");
        var album_song_count = $(this).attr("album_song_count");
        var singer_id = $(this).attr("singer_id");
        var singer_name = $(this).attr("singer_name");
        var issue_time = $(this).attr("issue_time");
        var user_id = $(this).attr("user_id");


        var params = {
            "album_id": album_id,
            'album_name': album_name,
            'album_pic': album_pic,
            'album_song_count': album_song_count,
            'singer_id': singer_id,
            'singer_name': singer_name,
            'user_id': user_id,
            'issue_time': issue_time,
            'active_like': active_like,
        };


        $.ajax({
                url: "/albums_detail/",
                type: "post",
                data: JSON.stringify(params),
                contentType: "application/json",
                headers: {
                    "X-CSRFToken": getCookie("csrf_token")
                },
                success: function (resp) {
                    if (resp.errno == "0") {

                        alert(resp.errmsg);
                        window.location.reload()  //刷新页面
                    } else {
                        alert(resp.errmsg);
                    }
                }
            })


    });

    $(".del_album").click(function (e) {
        var del_like = 2;
        var album_id = $(this).attr("album_id");
        var user_id = $(this).attr("user_id");
        var params = {
            "del_like": del_like,
            'album_id': album_id,
            'user_id': user_id,
        };


        $.ajax({
                url: "/albums_detail/",
                type: "delete",
                data: JSON.stringify(params),
                contentType: "application/json",
                headers: {
                    "X-CSRFToken": getCookie("csrf_token")
                },
                success: function (resp) {
                    if (resp.errno == "0") {
                        alert(resp.errmsg);
                        window.location.reload()  //刷新页面
                    } else {
                        alert(resp.errmsg);
                    }
                }
            })


    });

    $(".playlist_songs").click(function (e) {

        var singer_id = $(this).attr("playlist_singer_id");
        var singer_name = $(this).attr("playlist_singer_name");
        var song_name = $(this).attr("playlist_song_name");
        var song_id = $(this).attr("playlist_song_id");
        var song_album = $(this).attr("playlist_song_album");
        var song_album_id = $(this).attr("playlist_song_album_id");
        var user_id = $(this).attr("playlist_user_id");
        var song_list_id = $(this).attr("playlist_song_list_id");


        var params = {
            "song_name": song_name,
            'singer_name': singer_name,
            'singer_id': singer_id,
            'user_id': user_id,
            'song_id': song_id,
            'song_album': song_album,
            'song_album_id': song_album_id,
            'song_list_id': song_list_id,

        };


        $.ajax({
                url: "/song_details/",
                type: "post",
                data: JSON.stringify(params),
                contentType: "application/json",
                headers: {
                    "X-CSRFToken": getCookie("csrf_token")
                },
                success: function (resp) {
                    if (resp.errno == "0") {

                        alert(resp.errmsg);
                        window.location.reload()  //刷新页面
                    } else {
                        alert(resp.errmsg);
                    }
                }
            })


    });

    // 修改密码
    $(".contact-form").submit(function (e) {
        // 阻止表单的默认提交行为
        e.preventDefault();
        alert("检测代码");
        var old_password = $('#old_password').val();
        var password1 = $('#password1').val();
        var password2 = $('#password2').val();
        var params = {
            "old_password":old_password,
            "password1":password1,
            "password2":password2,
        };

        if (password1 != password2) {
            alert('输入的两次密码不一致!');
            return;
        }

        $.ajax({
            url: "/contact/",
            type: "post",
            data: JSON.stringify(params),
            contentType: "application/json",
            headers: {
                "X-CSRFToken": getCookie("csrf_token")
            },
            success: function (resp) {
                if (resp.errno == "0") {
                    alert(resp.errmsg);
                    window.location.href = '/register/';
                } else {
                    alert(resp.errmsg);
                }
            }
        })


    });


    $(".user_modify").submit(function (e) {
        // 阻止表单的默认提交行为
        e.preventDefault();

        //获取数据
        var username = $('#username').val();
        var desc = $('#desc').val();
        var gender = $('#gender').val();
        var date_time = $('#date_time').val();
        var user_avatar = $('#user_avatar').val();


        // 组织参数
        var params = {
            "username": username,
            "desc": desc,
            "gender": gender,
            "date_time": date_time,
            "user_avatar": user_avatar,
        };

        // TODO: 请求修改用户密码
        $.ajax({
            url: "/user-modify/",
            type: "post",
            data: JSON.stringify(params),
            contentType: "application/json",
            headers: {
                "X-CSRFToken": getCookie("csrf_token")
            },
            success: function (resp) {
                if (resp.errno == "0") {
                    // `登录成功
                    alert(resp.errmsg);
                    window.location.reload()
                } else {
                    // `登录失败
                    alert(resp.errmsg);
                }
            }
        })

    });



});
